#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <string>
#include <stdexcept>
#include <bits/stdc++.h>
#include "outcsv.hpp"
using namespace std;

long double stringToLongDouble(const std::string& str) {
    long double result;
    try {
        result = std::stold(str);
    } catch (const std::invalid_argument& ia) {
        std::cerr << "Invalid argument: " << ia.what() << '\n';
        return 0.0L; // 返回0.0L或适当的错误值
    } catch (const std::out_of_range& oor) {
        std::cerr << "Out of range: " << oor.what() << '\n';
        return std::numeric_limits<long double>::infinity()
                   * (str[0] == '-' ? -1.0L : 1.0L); // 返回无穷大或负无穷大
    }
    return result;
}
int main() {
    std::ifstream file("/home/dmf/test/ttzzbb/final_h3_point_22km.csv"); // 打开文件
    ofstream outcsvFile("/home/dmf/test/ttzzbb/output_22km.csv"); // 创建新的CSV文件
    if (!file.is_open() || !outcsvFile.is_open()) {
        std::cerr << "Unable to open file." << std::endl;
        return 1;
    }

    std::string line;
    map<string, vector<long double>> dataMap; // 创建map来存储数据
    map<string,int> num;
    string mission_id_new;
    while (std::getline(file, line)) { // 逐行读取
        std::istringstream s(line);
        std::string cell;
        std::vector<std::string> row;
        vector<long double> gra;
        // 按逗号分割每行
        while (std::getline(s, cell, ',')) {
            row.push_back(cell);
        }

        // 将第一列作为键，其余列作为值
        if (!row.empty()) {
            num[row[0]]++;
            string mission_id = row[0];
            string mission_num = row[1];
            mission_id_new = mission_id + "_" + mission_num;
            /*
            double lat = stringToLongDouble(row[2]);
            double lon = stringToLongDouble(row[3]);
            if(dataMap.find(row[0]) == dataMap.end()){
                gra.emplace_back(lat);
                gra.emplace_back(lon);
                dataMap[row[0]] = gra;
            }
            else{
                auto first = dataMap[row[0]];
                gra.emplace_back(lat + first[0]);
                gra.emplace_back(lon + first[1]);
                dataMap[row[0]] = gra;
            }
            */

        }
    }

    file.close(); // 关闭文件
    /*
    for(const auto& mis : num){
        dataMap[mis.first][0] =  dataMap[mis.first][0] / mis.second;
        dataMap[mis.first][1] =  dataMap[mis.first][1] / mis.second;
    }
    // 打印map中的数据
    for (const auto& pair : dataMap) {
        std::cout << "Key: " << pair.first << ", num: "<< num[pair.first] << ", Values: ";
        for (const auto& value : pair.second) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    }
    */
    // 写入新的CSV文件
    for (const auto& pair : dataMap) {
        outcsvFile << pair.first; // 写入键

        for (size_t i = 0; i < pair.second.size(); i++) { // 写入值
            outcsvFile << "," << pair.second[i];
        }
        outcsvFile << "," <<num[pair.first];
        outcsvFile << std::endl; // 换行
    }

    outcsvFile.close();

    return 0;
}